import {RoomMode} from "../Constants";

export class RoomData {
    roomid:number;
    gameid:number;
    name:number;
    level:number;
    host_ip:string;
    host_port:number;
    roommode:number;
    playtype:number;
    status:number;
    disabled:boolean;
    getAddress(){
        return this.host_ip+":"+this.host_port;
    }
}

export class RoomList {
    private static list:Array<RoomData>
    static init(roomlist){
        RoomList.list = [];
        for(let i = 0 ;i<roomlist.length;i++){
            let roomData = new RoomData();
            roomData.roomid = roomlist[i].roomid;
            roomData.gameid = roomlist[i].gameid;
            roomData.name = roomlist[i].name;
            roomData.level = roomlist[i].level;
            roomData.host_ip = roomlist[i].host_ip;
            roomData.host_port = roomlist[i].host_port;
            roomData.roommode = roomlist[i].roommode;
            roomData.playtype = roomlist[i].playtype;
            roomData.status = roomlist[i].status;
            roomData.disabled = roomlist[i].disabled;
            console.log("RoomList",roomData)
            RoomList.list.push(roomData);
        }
        console.log("RoomList",RoomList.list)
    }

    private static getRoomList(roomMode:RoomMode,level?:number):Array<RoomData>{
        console.log("getRoomListByRoomMode",roomMode,level)
        let tmp = [];
        for(let i = 0 ;i<this.list.length;i++){
            if(RoomList.list[i].roommode != roomMode){
                continue;
            }
            if(level != null && level != RoomList.list[i].level){
                continue;
            }
            tmp.push(this.list[i])
        }
        console.log("getRoomListByRoomMode",tmp)
        return tmp;
    }

    static getNormalRoomList(level?:number):Array<RoomData>{
        let normal = RoomList.getRoomList(RoomMode.NORMAL,level);
        let fish = RoomList.getRoomList(RoomMode.FISH,level);
        return normal.concat(fish);
    }

    static getFriendRoomList():Array<RoomData>{
        return RoomList.getRoomList(RoomMode.FRIEND);
    }

    static getNormalRoom(level?:number):RoomData{
        let tmp = RoomList.getNormalRoomList(level);
        let index =  Math.floor(Math.random()*tmp.length);
        return tmp[index];
    }

    static getFriendRoom():RoomData{
        let tmp = RoomList.getFriendRoomList();
        let index =  Math.floor(Math.random()*tmp.length);
        return tmp[index];
    }

    static getRoomByID(roomid?:number):RoomData{
        for(let i = 0 ;i<this.list.length;i++){
            if(RoomList.list[i].roomid == roomid){
                return RoomList.list[i];
            }
        }
        return null;
    }
}
